<template>
  <div class="mod-config editViewContainer">
    <split-pane class="splitPane" :min-percent='10' :default-percent='10' split="vertical">
      <template slot="paneL">
        <div class="treeViewContainer">
          <tree-view ref="treeView" :treeData="productDeviceTreeList" :checkChangedHandler="getDataList"></tree-view>
        </div>
      </template>
      <template slot="paneR">
        <table-view ref="view"
                    :searchAttrs="searchAttrs"
                    :primaryKey="primaryKey"
                    :listApiName="listApiName"
                    :saveAuthName="saveAuthName"
                    :showAttrs="showAttrs"
                    :optionWidth="200"
                    :exportPlantFunc="exportPlantFunc"
                    :rowClassName="rowClassName"

        >
          <template v-slot:header>
            <el-button size="small" type="success" @click="exportOrderOnlinePlan">
              导出排单计划
            </el-button>
          </template>
          <template v-slot:foot="{scope}">
            <el-tooltip v-if="isAuth(sendOrderLeadApprovalAuthName)" class="item" effect="dark" content="修改订单状态" placement="bottom" :hide-after="1800"
                        :open-delay="800">
              <el-button type="primary" icon="el-icon-brush" size="small"
                         @click="openOrderStatusDialog(R.prop('row', scope))"></el-button>
            </el-tooltip>
<!--            <el-tooltip v-if="isAuth(boxOrderAuthName)" class="item" effect="dark" content="修改纸箱订购状态" placement="bottom"
                        :hide-after="1800" :open-delay="800">
              <el-button type="warning" icon="el-icon-box" size="small"
                         @click="openBoxOrderStatusDialog(R.prop('row', scope))"></el-button>
            </el-tooltip>
            <el-tooltip v-if="isAuth(boxOrderAuthName)" class="item" effect="dark" content="添加损耗数" placement="bottom"
                        :hide-after="1800" :open-delay="800">
              <el-button size="small" type="text" @click="openOrderIssuesNumberDialog(R.prop('row', scope))">
                损耗数
              </el-button>
            </el-tooltip>
            <el-tooltip v-if="isAuth(boxOrderAuthName)" class="item" effect="dark" content="修改标记膏霜大口" placement="bottom"
                        :hide-after="1800" :open-delay="800">
              <el-button size="small" type="text" @click="openBigMouthDialog(R.prop('row', scope))">
                标记大口
              </el-button>
            </el-tooltip>-->
          </template>
        </table-view>
      </template>
    </split-pane>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <el-dialog
      title="选择当前订单的状态"
      :visible.sync="dialogVisible"
      width="30%">
      <el-radio-group v-model="status">
        <el-radio :label="0">待配货</el-radio>
        <el-radio :label="1">暂时下线</el-radio>
        <el-radio :label="2">加急</el-radio>
        <el-radio :label="3">正常</el-radio>
        <el-radio :label="4">生产中</el-radio>
        <el-radio :label="5">生产完成</el-radio>
        <el-radio :label="30">上线中</el-radio>
        <el-radio :label="32">加急上线</el-radio>
        <el-radio :label="90">客户待确认</el-radio>
        <el-radio :label="99">取消</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyOrderStatus">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="选择当前纸箱下单的状态"
      :visible.sync="boxOrderDialogVisible"
      width="30%">
      <el-radio-group v-model="boxOrderStatusRadio">
        <el-radio :label="0">未下单</el-radio>

        <el-radio :label="2">已下单</el-radio>

        <el-radio :label="5">正在加工</el-radio>

        <el-radio :label="10">入库完成</el-radio>

        <el-radio :label="15">改用托盘</el-radio>

        <el-radio :label="20">来料加工</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="boxOrderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyBoxOrderStatus">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改损耗数量"
      :visible.sync="orderIssuesNumberVisible"
      width="30%">
      <el-input-number v-model="orderIssuesNumber">
      </el-input-number>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderIssuesNumberVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyOrderIssuesNumber">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改标记膏霜大口"
      :visible.sync="bigMouthVisible"
      width="30%">
      <el-input v-model="bigMouth" placeholder="请输入标记膏霜大口">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bigMouthVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyBigMouth">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './orderdetail-add-or-update'
import tableView from '@/components/tableView'
import treeView from '@/components/treeView'
import basicMixins from '@/utils/commonViewMixins'
// import ProductinfoAddOrUpdate from '@/views/modules/product/productinfo-add-or-update'
// import ProductplannoticeAddOrUpdate from '@/views/modules/product/productplannotice-add-or-update'
// import ClientInfoAddOrUpdate from '@/views/modules/client/clientinfo-add-or-update'
// import DeepCategoryAddOrUpdate from '@/views/modules/deep/deepcategory-add-or-update'

import {mRequest} from '@/utils/request'
import {exportHandle, isAuth} from '@/utils'

export default {
  mixins: [basicMixins],
  data() {
    return {
      isAuth: isAuth,
      status: 0,
      bigMouth: '',
      boxOrderStatusRadio: 0,
      orderIssuesNumber: 0,
      dialogVisible: false,
      orderIssuesNumberVisible: false,
      boxOrderDialogVisible: false,
      productPlanNoticeVisible: false,
      productInfoVisible: false,
      clientInfoVisible: false,
      deepCategoryInfoVisible: false,
      bigMouthVisible: false,
      orderPlanNoticeVisible: false,
      productDeviceList: [],
      deviceIdList: [],
      orderDetailList: [],
      productInfoList: [],
      modelInfoList: [],
      tableListData: [],
      deepCategoryList: [],
      productDeviceTreeList: [],
      deviceId: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      onlineTime: '',
      searchAttrs: {
        productNo: {
          label: '产品名称',
          placeholder: '请输入产品名称',
        },
        // sendOrderLeadApproval:{
        //   label: '排单审核',
        //   placeholder: '排单审核',
        //   type: 'list',
        //   list: [
        //     {id: 9, name: '待审核'},
        //     {id: 10, name: '驳回'},
        //     {id: 11, name: '已审核'}
        //   ]
        // },
        // plantStatus: {
        //   label: '导出状态',
        //   clearable: true,
        //   placeholder: '请选择导出状态',
        //   type: 'list',
        //   list: [],
        // },
        // specialMark:{
        //   label: '箱唛',
        //   clearable: true,
        //   placeholder: '请选择箱唛',
        //   type: 'list',
        //   list: [],
        // },

        // manufacturerType:{
        //   label: '深加工在厂',
        //   clearable: true,
        //   placeholder: '深加工在厂',
        //   type: 'list',
        //   list: [],
        // },
    /*    createUser: {
          label: '销售',
          clearable: true,
          type: 'autoComplete',
          placeholder: '请选择销售人员',
          querySearchApiName: 'systemApi/listSaleUser'
        },*/
        productWeight: {
          label: '产品重量',
          placeholder: '请输入重量'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        },
        supplierId: {
          type: 'func',
          dataFunc: () => {
            return this.$refs.treeView.$refs.treeForm.getCheckedKeys()[0]
          }
        },

    /*    clientId: {
          label: '客户名称',
          clearable: true,
          type: 'autoComplete',
          placeholder: '请输入客户名称',
          querySearchApiName: 'clientApi/getClientList'
        },*/


      },
      primaryKey: 'id',
      listApiName: 'purchaseApi/lisPurchaseProductBySupplierId',
      deleteApiName: 'purchaseApi/deleteOrderDetail',
      saveAuthName: 'purchase:purchasepart:save',
      deleteAuthName: 'purchase:purchasepart:delete',
      exportAuthName: 'purchase:purchasepart:save',
      sendOrderLeadApprovalAuthName: 'order:orderdetail:sendOrderLeadApproval',
      showAttrs: {
        orderNo: {
          label: '采购编号',
          width: 140,
          type: 'labelWithStatus',
          statusAttr: 'messageStatus',
          items: [
            {value: 0, text: '未导', tagType: 'danger'}
          ]
        },
        status: {
          label: '状态',
          type: 'radio',
          width: 100,
          items: [
            {value: 0, text: '待采购', tagType: 'info'},
            {value: 2, text: '采购进行中'},
            {value: 3, text: '采购逾期预警',tagType:'warning'},
            {value: 4, text: '采购已逾期', tagType: 'danger'},
            {value: 6, text: '采购暂停', tagType: 'info'},
            {value: 7, text: '入库进行中', tagType: 'warning'},
            {value: 8, text: '入库完成',tagType:'success'},
            {value: 17, text: '挑选', tagType: 'warning'},
            {value: 18, text: '合格', tagType: 'success'},
            {value: 19, text: '不合格', tagType: 'danger'},
            {value: 20, text: '特采', tagType: 'warning'},
            {value: 99, text: '采购取消', tagType: 'danger'},
          ]
        },
        // sendOrderLeadApproval:{
        //   label: '排单审核',
        //   type: 'radio',
        //   items: [
        //     {value: 9, text: '待审核', tagType: 'danger'},
        //     {value: 10, text: '驳回',tagType: 'warning'},
        //     {value: 11, text: '已审核',tagType: 'success'}
        //   ]
        // },
        createTime: {label: '下单时间', type: 'date', width: 100},
        productNo: {
          label: '产品编号',
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo',
         width: 150
        },
        purchaseNumber: {label: '采购数量'},
        productWeight: {label: '克重'},
        productVolume: {label: '容量'},
        putInStorageNumber: {label: '已入库数'},
        bottleOutDiameter: {label: '口径'},
        modelNo: {label: '模具编号'},

/*        processProductNumberSum: {label: '生产数量(每日)'},
        putInStorageNumber: {label: '入库数量'},*/
        // boxSupplyWay: {
        //   label: '包装供应',
        //   type: 'radio',
        //   items: [
        //     {text: '纸箱', value: 0, tagType: 'success'},
        //     {text: '托盘', value: 1},
        //     {text: '自供', value: 2, tagType: 'danger'}
        //   ]
        // },
        // boxOrderStatus: {
        //   label: '纸箱下单',
        //   type: 'radio',
        //   items: [
        //     {text: '未下单', value: 0, tagType: 'danger'},
        //     {text: '已下单', value: 1, tagType: 'success'},
        //     {text: '改托盘', value: 2, tagType: 'warning'},
        //     {text: '托盘', value: 3},
        //     {text: '自供', value: 4}
        //   ]
        // },

/*
        salesperson: {label: '销售'},
*/
        // boxInfoList: {label: '纸箱', width: 150},
        remark: {label: '备注', width: 130},
        // deviceId: {
        //   label: '上机',
        //   type: 'radio',
        //   items: [
        //     {value: 1, text: '1#8S', tagType: 'danger'},
        //     {value: 2, text: '2#六单', tagType: 'success'},
        //     {value: 3, text: '3#六双'},
        //     {value: 4, text: '4#六单', tagType: 'success'},
        //     {value: 5, text: '5#可变8S', tagType: 'warning'},
        //     {value: 6, text: '6#可变8S', tagType: 'warning'},
        //     {value: 7, text: '7#线压机', tagType: 'info'},
        //     {value: 99, text: '待分配', tagType: 'info'}
        //   ]
        // },
        modelShelfNo: {label:'模具位置'},
        // onlineTime: {label: '预计送货时间', type: 'date', width: 95},
        // deviceWorkbenchName:{
        //   label: '机台',
        //   width: 120
        // },
        boxCartonSize: {label: '箱规(cm)'},
        boxPackImage: {label: '箱唛图片', type: 'image'},
/*
        bigMouth: {label: '标记膏霜'},
*/
        deliveryTime: {label: '交货时间', type: 'date', width: 95},
        bottleCraft: {label: '工艺'},
        // deepTypeStr: {label: '深加工'},
        // manufacturerType: {
        //   label: '深加工在厂',
        //   type: 'radio',
        //   items: [
        //     {value: 0, text: '在厂',tagType: 'success'},
        //     {value: 1, text: '不在厂', tagType: 'danger'}
        //   ]
        // },
/*        headNeckHeight: {label: '头径高度'},
        bottleLength: {label: '长度'},
        bottleWeight: {label: '宽度'},
        bottleHeight: {label: '高度'},*/
        supplierName: {label: '供应商',width: 120},
        unitPrice: {
          label: '单价',
          showAuthName: 'order:unitprice:show'
        },
      }
    }
  },
  mounted() {
    this.productDeviceTreeListInit()
  },
  methods: {
    rowClassName({row, rowIndex}) {
      switch (row.status) {
        case 0:
          return 'info-row'
/*        case 2:
          return 'primary-row'*/
        case 3:
          return 'warning-row'
        case 4:
          return 'danger-row'
        case 5:
          return 'danger-row'
        case 7:
          return 'warning-row'
        case 8:
          return 'success-row'
        case 20:
          return 'special-row'
        default:
          return ''

      }
    },

    productDeviceTreeListInit() {
      mRequest('supplierApi/getSupplierListByParent', {}, (res) => {
        this.productDeviceList = this.R.pathOr([], ['data', 'list'], res)
        const productDeviceTreeList = [{
          id: '',
          name: '供应商',
          nodeType: 0,
          children: []
        }]
        productDeviceTreeList[0].children = this.productDeviceList
        this.productDeviceTreeList = productDeviceTreeList;
      })
/*      this.searchAttrs['plantStatus'].list = [
        {
          id: 0, name: '未导出'
        },
        {
          id: 1, name: '已导出'
        }
      ]
      this.searchAttrs['specialMark'].list = [
        {
          id: 0, name: '普英唛'
        },
        {
          id: 1, name: '专用唛'
        },
        {
          id: 2, name: '普英唛+H'
        }
      ]*/
      // this.searchAttrs['manufacturerType'].list = [
      //   {
      //     id: 0, name: '在厂'
      //   },
      //   {
      //     id: 1, name: '不在厂'
      //   }
      // ]
 /*     this.searchAttrs['sendOrderLeadApproval'].list=[
        {id: 9, name: '待审核'},
        {id: 10, name: '驳回'},
        {id: 11, name: '已审核'}
      ]*/
    },
    openOrderStatusDialog(order) {
      this.dialogVisible = true
      this.modifyOrder = order
      const {status} = order

      this.status = status
    },
    modifyOrderStatus() {
      if (this.modifyOrder.status === this.status) {
        return
      }
      this.modifyOrder.status = this.status

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.status = 0
        this.dialogVisible = false
        this.getDataList()
      })
    },
    modifyBigMouth() {
      if (this.modifyOrder.bigMouth === this.bigMouth) {
        return
      }
      this.modifyOrder.bigMouth = this.bigMouth

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.bigMouth = ''
        this.bigMouthVisible = false
        this.getDataList()
      })
    },
    openBigMouthDialog(order) {
      this.bigMouthVisible = true
      this.modifyOrder = order
      const {bigMouth} = order

      this.bigMouth = bigMouth
    },
    openBoxOrderStatusDialog(order) {
      this.boxOrderDialogVisible = true
      this.modifyOrder = order
      const {boxOrderStatus} = order

      this.boxOrderStatusRadio = boxOrderStatus
    },
    modifyBoxOrderStatus() {
      if (this.modifyOrder.boxOrderStatus === this.boxOrderStatusRadio) {
        return
      }
      this.modifyOrder.boxOrderStatus = this.boxOrderStatusRadio

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.boxOrderStatusRadio = 0
        this.boxOrderDialogVisible = false
        this.getDataList()
      })
    },
    modifyOrderIssuesNumber() {
      if (this.modifyOrder.orderIssuesNumber === this.orderIssuesNumber) {
        return
      }
      this.modifyOrder.orderIssuesNumber = this.orderIssuesNumber

      mRequest('orderApi/updateOrderDetail', this.modifyOrder, () => {
        this.modifyOrder = null
        this.orderIssuesNumber = 0
        this.orderIssuesNumberVisible = false
        this.getDataList()
      })
    },
    openProductPlanNotice(id) {
      this.productPlanNoticeVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productPlanNotice)) {
          this.$refs.productPlanNotice.init(null, {orderDetailId: id}, () => {
            this.$router.push({name: 'product-productplannotice'})
          })
        }
      })
    },
    openProductInfoDialog(cb) {
      this.productInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productInfo)) {
          this.$refs.productInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openClientInfoDialog(cb) {
      this.clientInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.clientInfo)) {
          this.$refs.clientInfo.init(null, {status: 1}, cb)
        }
      })
    },
    openDeepCategoryInfoDialog(cb) {
      this.deepCategoryInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.deepCategory)) {
          this.$refs.deepCategory.init(null, {status: 1}, cb)
        }
      })
    },
    openOrderIssuesNumberDialog(order) {
      this.orderIssuesNumberVisible = true
      this.modifyOrder = order
      const {orderIssuesNumber} = order

      this.orderIssuesNumber = orderIssuesNumber
    },
    exportPlantFunc(rows) {
      const ids = rows.map(item => {
        return item[this.primaryKey]
      })
      this.$confirm('是否直接导出改计划?', '提示', {
        confirmButtonText: '导出',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        exportHandle('productApi/getProductPlanNoticeExport', {orderDetailIdList: ids})
        this.getDataList()
      }).catch(() => {
      })

    },
    initProductDeviceListByParent() {
      mRequest('productApi/getProductDeviceListByParent', {}, (res) => {
        this.productDeviceList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initOrderDetailList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initProductInfoList() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    updatePlanOrderStatus() {
      mRequest('orderApi/updatePlanOrderStatus', {
        'onlineTime': this.onlineTime,
        'deviceId': this.deviceId,
        'tableListData': this.tableListData
      }, res => {
        const data = res.data

        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.orderPlanNoticeVisible = false
              this.getDataList()
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    exportOrderOnlinePlan() {
      console.log("导出生产计划")
    }
  },
  components: {
    // ProductplannoticeAddOrUpdate,
    // ProductinfoAddOrUpdate,
    // ClientInfoAddOrUpdate,
    // DeepCategoryAddOrUpdate,
    AddOrUpdate,
    tableView,
    treeView
  }
}
</script>
<style scoped>
.el-card /deep/ .el-card__body {
  padding: 6px;
}

.el-card /deep/ .el-card__header {
  padding: 3px 20px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-size: 16px;
}
</style>
